/* $Id: ProblemUserScoreEntry.java 134 2011-08-27 16:48:38Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.datatypes.scores;

import java.util.Date;

import javax.cache.Cache;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import alt.djudge.frontend.server.datatypes.SubmissionEntry;
import alt.djudge.frontend.server.models.CacheModel;


@PersistenceCapable(detachable="true")
@Inheritance(customStrategy = "complete-table")
public class ProblemUserScoreEntry extends AbstractScoreEntry implements
		UserScoreInterface, ProblemScoreInterface
{

	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	@Override
	public Long getId()
	{
		return id;
	}
	
	@Override
	public void setId(Long id)
	{
		this.id = id;
	}
	
	@Persistent
	protected Long userId;

	@Persistent
	protected Long contestProblemId;

	@Persistent
	protected Integer maxScore = -1;

	@Persistent
	protected Date maxScoreFirstDate;

	@Persistent
	protected Long maxScoreFirstTime = -1L;

	@Persistent
	protected Long maxScoreId;

	public void setContestProblemId(Long contestProblemId)
	{
		this.contestProblemId = contestProblemId;
	}

	public Long getContestProblemId()
	{
		return contestProblemId;
	}

	public void setMaxScore(Integer maxScore)
	{
		this.maxScore = maxScore;
	}

	public Integer getMaxScore()
	{
		return maxScore;
	}

	public void setMaxScoreFirstDate(Date maxScoreFirstDate)
	{
		this.maxScoreFirstDate = maxScoreFirstDate;
	}

	public Date getMaxScoreFirstDate()
	{
		return maxScoreFirstDate;
	}

	public void setMaxScoreFirstTime(Long maxScoreFirstTime)
	{
		this.maxScoreFirstTime = maxScoreFirstTime;
	}

	public String getMaxScoreFirstTimeStr()
	{
		return "" + ((maxScoreFirstTime + 59) / 60) + " хв";
	}
	
	public Long getMaxScoreFirstTime()
	{
		return maxScoreFirstTime;
	}

	public void setMaxScoreId(Long maxScoreId)
	{
		this.maxScoreId = maxScoreId;
	}

	public Long getMaxScoreId()
	{
		return maxScoreId;
	}

	public Boolean getAccepted()
	{
		return uniqueAcceptedCount > 0;
	}

	@Override
	public void prepareForRejudge()
	{
		super.prepareForRejudge();
		totalTime = -1L;
		maxScore = -1;
		maxScoreFirstDate = null;
		maxScoreFirstTime = -1L;
		maxScoreId = null;
	}

	public ProblemUserScoreEntry()
	{
		// TODO Auto-generated constructor stub
	}

	public ProblemUserScoreEntry(Long contestProblemId, Long userId)
	{
		this.userId = userId;
		this.contestProblemId = contestProblemId;
	}

	@Override
	public Long getUserId()
	{
		return userId;
	}

	@Override
	public void setUserId(Long userId)
	{
		this.userId = userId;
	}

	@Override
	public void clearCacheCustom()
	{
		Cache cache = CacheModel.getStaticCache();
		String keyProblemUser = CacheModel.getProblemUserScoreEntryKey(
				contestProblemId, userId);
		cache.remove(keyProblemUser);
		// String keyProblem =
		// CacheModel.getProblemUserScoreEntryKey(contestProblemId, );
		// //cache.remove(keyProblem);
	}

	@Override
	public void addSubmission(SubmissionEntry s)
	{
		if (s.isPending())
			pendingCount++;
		else if ("CE".equalsIgnoreCase(s.getJudgement()))
		{

		}
		else
		{
			activeCount++;
			lastSubmittedDate = s.getSubmissionTime();
			lastSubmittedTime = s.getContestTime();
		}
		if (s.getScore() > maxScore)
		{
			maxScore = s.getScore();
			maxScoreFirstDate = s.getSubmissionTime();
			maxScoreFirstTime = s.getContestTime();
			maxScoreId = s.getId();
		}
		if (s.isAccepted())
		{
			if (acceptedCount == 0)
			{
				uniqueAcceptedCount++;
				rejectedBeforeAcceptedCount = uniqueRejectedCount;
				totalTime = s.getContestTime() + 20 * 60 * rejectedBeforeAcceptedCount;
			}
			acceptedCount++;
			lastAcceptedDate = s.getSubmissionTime();
			lastAcceptedTime = s.getContestTime();
		}
		else
		{
			if (acceptedCount == 0)
				uniqueRejectedCount++;
			rejectedCount++;
		}
	}
}
